package app;

import java.io.IOException;
import java.util.Scanner;

public class Main {
	Scanner input;
	DataSet dataSet;
	String[] attributeValues;
	boolean ponderateFlag;
	boolean laplaceFlag;
	int k;
	
	public Main() {
		input = new Scanner(System.in);
	}
	
	public void close() {
		input.close();
	}

	public static void main(String[] args) {
		if (args.length == 0 || args.length > 1) {
			System.out.println("Usage: master-aia 'tema'");
			System.out.println("Where 'tema' can be: tema2, tema3, tema4");
			System.exit(-1);
		}
		
		Main main = new Main();
		if (args[0].equals("tema2")) {
			System.out.println("----TEMA 2----");
//			main.testDataSet();
			main.readDataSet();
			main.readImpurityMeasure();
			main.tema2();
		} else if (args[0].equals("tema3")) {
			System.out.println("----TEMA 3----");
//			main.testDataSet();
			main.readDataSet();
			System.out.println("Setul de date este: ");
			System.out.println(main.dataSet);
			main.readInstance();
			main.readK();
			main.readPonderateFlag();
//			main.testInstance();
			main.tema3();
		} else if (args[0].equals("tema4")) {
			System.out.println("----TEMA 4----");
//			main.testDataSet();
			main.readDataSet();
			System.out.println("Setul de date este: ");
			System.out.println(main.dataSet);
			main.readInstance();
			main.readFlagLaplace();
//			main.testInstance();
			main.tema4();
		}
		
		main.close();
	}
	
	public void readDataSet() {
		String fileName;

		System.out.print("Introduceti numele fisierului: ");
		fileName = input.nextLine();

		dataSet = DataSetReader.read(fileName + ".cds");
	}
	
	public void readImpurityMeasure() {
		String impurityMeasure;
		
		System.out.print("Introduceti masura de impuritate (Gini / Entropy): ");
		impurityMeasure = input.nextLine();

		if (impurityMeasure.equalsIgnoreCase("Gini")) {
			dataSet.setImpurityMeasure(ImpurityMeasure.Gini);
		} else if (impurityMeasure.equalsIgnoreCase("Entropy")) {
			dataSet.setImpurityMeasure(ImpurityMeasure.Entropy);
		}
	}
	
	public void testDataSet() {
		dataSet = DataSetReader.read("cheat.cds");
		dataSet.setImpurityMeasure(ImpurityMeasure.Gini);
	}
	
	public void readInstance() {	
		try {
			System.out.println("Introduceti valorile pentru noua instanta");
			System.out.println("(separate prin virgula, fara valoarea clasei):");
			String strValues = input.nextLine();
			
			// add a fake class value
			strValues += ",??";
			
			attributeValues = strValues.split(",");
			if(attributeValues.length != dataSet.getAttributeSet().size()) 
				throw new IOException("Numarul de valori nu coincide cu numarul de atribute!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void readPonderateFlag() {
		System.out.println("Folositi ponderare?: (da/nu)");
		String line = input.nextLine();
		if(line.equalsIgnoreCase("nu")) {
			ponderateFlag = false;
		} else 
			if(line.equalsIgnoreCase("da")) {
				ponderateFlag = true;
			}
	}
	
	public void readFlagLaplace() {
		System.out.println("Folositi corectia Laplace?: (da/nu)");
		String line = input.nextLine();
		if(line.equalsIgnoreCase("nu")) {
			laplaceFlag = false;
		} else 
			if(line.equalsIgnoreCase("da")) {
				laplaceFlag = true;
			}
	}
	
	public void readK() {
		System.out.println("Introduceti k (numarul celor mai apropiati vecini): ");
		k = Integer.parseInt(input.nextLine());
	}
	
	public void testInstance() {
		String strValues = "no,married,80,??";
		
		attributeValues = strValues.split(",");
		k = 3;
		ponderateFlag = true;
	}
	
	public void tema2() {
		DecisionTree decisionTree = new DecisionTree(dataSet);
		decisionTree.printTree();
	}
	
	public void tema3() {
		Instance newInstance = new Instance(dataSet.getAttributeSet());
		newInstance.populateWithoutUpdate(attributeValues);
		
		dataSet.kNearestNeighbour(newInstance, k, ponderateFlag);
	}
	
	public void tema4() {
		Instance newInstance = new Instance(dataSet.getAttributeSet());
		newInstance.populateWithoutUpdate(attributeValues);
		dataSet.setFlagLaplace(laplaceFlag);
		dataSet.naiveBayes(newInstance);
	}
}